package guiLayer.extensions;

import java.util.ArrayList;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import modelLayer.Forest;
import modelLayer.Owner;
import modelLayer.Tree;

public class OwnerTreeModel implements TreeModel{
	private ArrayList<Owner> owners;
	private String root;
	
	public OwnerTreeModel(ArrayList<Owner> owners){
		this.owners = owners;
		this.root = "Owners";
	}

	
	public void addTreeModelListener(TreeModelListener arg0) {}

	
	public Object getChild(Object parent, int index) {
		Object o = null;
		
		if(parent instanceof String){
			if(owners.size() < index){
				return null;
			}else{
				o = owners.get(index);
			}
		}else if(parent instanceof Owner){
			ArrayList<Forest> forests = ((Owner)parent).getForests();
			if(forests.size() < index){ 
				return null;
			}else{
				o = forests.get(index);
			}
		}else if(parent instanceof Forest){
			ArrayList<Tree> trees = ((Forest)parent).getTrees();
			if(trees.size() < index){ 
				return null;
			}else{
				o = trees.get(index);
			}
		}else{
			o = null;
		}
		
		return o;
	}

	
	public int getChildCount(Object parent) {
		int count = 0;
		if(parent instanceof String){
			count = owners.size();
		}else if(parent instanceof Owner){
			count = ((Owner)parent).getForests().size();
		}else if(parent instanceof Forest){
			count = ((Forest)parent).getTrees().size();
		}
		
		return count;
	}

	
	public int getIndexOfChild(Object parent, Object child) {
		int res = -1;
		if(parent instanceof String){
			res = owners.indexOf(child);
		}else if(parent instanceof Owner){
			res = ((Owner)parent).getForests().indexOf(child);
		}else if(parent instanceof Forest){
			res = ((Forest)parent).getTrees().indexOf(child);
		}
		
		return res;
	}

	
	public Object getRoot() {
		return root;
	}

	
	public boolean isLeaf(Object node) {
		
		boolean res = true;
		
		if(node instanceof Owner || node instanceof Forest || node instanceof String){
			res = false;
		}
		
		return res;
	}

	
	public void removeTreeModelListener(TreeModelListener arg0) {}

	
	public void valueForPathChanged(TreePath arg0, Object arg1) {}
	
}
